package com.pengpeng.zookeeper.web;

import com.pengpeng.zookeeper.service.ZookeeperService;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 彭鹏
 * @date 2021/10/21.
 */
@RestController
public class ZookeeperController {
    @Autowired
    private ZookeeperService zookeeperService;

    @RequestMapping("/child")
    public List<String> getChildren(String node){
        List<String> childrenNode = zookeeperService.getChildrenNode("/"+node, true);
        return childrenNode;
    }
    @Autowired
    private ZooKeeper zooKeeper;
    @RequestMapping("/create")
    public String create(@RequestParam(value = "node") String node,@RequestParam(value = "ttl") long ttl) throws KeeperException, InterruptedException {
        String s = zooKeeper.create("/servers/" + node, node.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_WITH_TTL, null, ttl);
        return s;
    }
}
